package cn.ickck.law_paper_final.controller;

import cn.ickck.law_paper_final.model.entity.Gdb;
import cn.ickck.law_paper_final.model.entity.GdbBatch;
import cn.ickck.law_paper_final.model.response.GdbResponse;
import cn.ickck.law_paper_final.service.GdbBatchService;
import cn.ickck.law_paper_final.service.GdbService;
import cn.ickck.law_paper_final.utils.ExcelUtils;
import cn.ickck.law_paper_final.utils.JsonData;
import cn.ickck.law_paper_final.utils.RoundNoOfUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: ck
 * @Description:
 * @Date: Create in 21:33 2020/11/27
 */

@RestController
@RequestMapping("/api/pub/v2/gdb")
public class GdbController {
    @Autowired
    private GdbService gdbService;
    @Autowired
    private GdbBatchService gdbBatchService;

    //应该新建一个批次
    @Transactional
    @RequestMapping("insert")
    public JsonData insert(@RequestBody Gdb gdb) {

        if(gdb.getApplyDate()==null)
        {
            gdb.setApplyDate(new Date(200,4,20));
        }
        if(gdb.getBirthDate()==null)
        {
            gdb.setBirthDate(new Date(200,4,20));
        }
        if(gdb.getCourtDate()==null)
        {
            gdb.setCourtDate(new Date(200,4,20));
        }
        if(gdb.getDeadlineDate()==null)
        {
            gdb.setDeadlineDate(new Date(200,4,20));
        }
        if(gdb.getDivorcingDate()==null)
        {
            gdb.setDivorcingDate(new Date(200,4,20));
        }
        if(gdb.getExecuteDate()==null)
        {
            gdb.setExecuteDate(new Date(200,4,20));
        }
        if(gdb.getLitigationDate()==null)
        {
            gdb.setLitigationDate(new Date(200,4,20));
        }
        if(gdb.getWeddingDate()==null)
        {
            gdb.setWeddingDate(new Date(200,4,20));
        }
        if(gdb.getCoupleBirthDate()==null)
        {
            gdb.setCoupleBirthDate(new Date(200,4,20));
        }
        if(gdb.getLastEndDate()==null)
        {
            gdb.setLastEndDate(new Date(200,4,20));
        }
        if(gdb.getLoanGrantedDate()==null)
        {
            gdb.setLoanGrantedDate(new Date(200,4,20));
        }
        if(gdb.getLastEndDate()==null)
        {
            gdb.setLastEndDate(new Date(200,4,20));
        }
        if(gdb.getMortgageRegisterDate()==null)
        {
            gdb.setMortgageRegisterDate(new Date(200,4,20));
        }

        GdbBatch gdbBatch = new GdbBatch();
        gdbBatch.setDate(new Date());
        gdbBatch.setName("eg: name+date");
        String key = UUID.randomUUID().toString();
        gdbBatch.setKey(key);
        gdbBatchService.insert(gdbBatch);
        gdb.setKey(key);
        Integer flag = gdbService.insert(gdb);
        return JsonData.buildSuccess("插入成功");
    }


    @Transactional
    @RequestMapping("deleteById")
    public JsonData delete(Integer id) {
        Integer flag = gdbService.deleteById(id);

        return JsonData.buildSuccess("删除成功");
    }




    @RequestMapping("update")
    public JsonData update(@RequestBody Gdb gdb) {
        gdbService.update(gdb);
        return JsonData.buildSuccess("更新成功");
    }

    @RequestMapping("selectByPage")
    public JsonData SelectByPage(Integer page) {
        int count = gdbService.count();
        GdbResponse gdbResponse = new GdbResponse();
        gdbResponse.setCount(count);

        int start = count - page * 20 > 0 ? count - page * 20 : 0;
        int end = count - (page - 1) * 20 > 0 ? count - (page - 1) * 20 : count;
        List<Gdb> gdbList = gdbService.selectByPage(start, end);
        gdbResponse.setGdbList(gdbList);
        return JsonData.buildSuccess(gdbResponse);
    }


    //加入事务控制
    @Transactional
    @RequestMapping("importExcel")
    public JsonData importExcel(@RequestParam("file") MultipartFile file) throws Exception {

        //解析excel
        List<Gdb> gdbList = ExcelUtils.importExcel(file, 0, 1, Gdb.class);
        GdbBatch gdbBatch = new GdbBatch();
        String key = UUID.randomUUID().toString();
        gdbBatch.setName("eg:日期+时间");
        gdbBatch.setKey(key);
        gdbBatch.setDate(new Date());

        Integer flag = gdbBatchService.insert(gdbBatch);
        for(Gdb gdb:gdbList)
        {
            Class cls = gdb.getClass();
            Field[] fields = cls.getDeclaredFields();

            for (Field field:fields)
            {
                field.setAccessible(true);
                String type_name = field.getType().getSimpleName();

                if("String".equals(type_name))
                {

                    //test push

                    String str = (String) field.get(gdb);


                    if(str!=null&&str.length()>15&&(str.contains("99999")|str.contains("00000")))
                    {
                        str = RoundNoOfUtil.RoundNoOf(str,2);
                    }
                    field.set(gdb,str);

                }
                if("Date".equals(type_name))
                {
                    Date date = (Date)field.get(gdb);
                    if(date==null)
                    {
                        field.set(gdb,new Date(2000,4,20));

                    }

                }


            }
            gdb.setKey(key);
            gdbService.insert(gdb);



        }





        return JsonData.buildSuccess("批量录入成功");
    }
    @RequestMapping("findByNameOrKey")
    public JsonData findByNameOrKey(@Param("key") String key, @Param("page") Integer page) {
        GdbResponse gdbResponse = new GdbResponse();

        //只查询了单人的 没查询双人
        List<Gdb>  gdbList = gdbService.findByNameOrKey(key);
        gdbResponse.setCount(gdbList.size());
        List<Gdb> gdbList1 = new ArrayList<>();

        int start = gdbList.size() - page * 20 > 0 ? gdbList.size() - page * 20 : 0;
        int end = gdbList.size() - (page - 1) * 20 > 0 ? gdbList.size() - (page - 1) * 20 : gdbList.size();

        for (int i = start; i < end; i++) {
            gdbList1.add(gdbList.get(i));
        }
        gdbResponse.setGdbList(gdbList1);
        return JsonData.buildSuccess(gdbResponse);
    }


}
